StartGame = {}
StartGame.__index = StartGame
 
function StartGame.new()
    local instance = {

    }
    
    setmetatable(instance, StartGame)
    return instance
end

function StartGame:startNewGame(levelId)
  audio:stopMenuAudio()
  isVictoryScreen = false
  
  currentScreen = typesOfScreens["game"]
  MainLayer:clear()
  
  pl1 = Player.new()
  pl1:setName("Player 1")
  pl1:setOwnerName("playerOne")
  pl1:setGold(100)
  
  pl2 = Player.new()
  pl2:setName("Player 2")
  pl2:setOwnerName("playerTwo")
  pl2:setGold(100)
  
  currentPlayerTurn = pl1
  
  self:initProps(levelId)

  gamebar_sprite = MOAIGfxQuad2D.new()
  gamebar_sprite:setTexture("resources/graphics/ingame_menu/gamebar.png")
  gamebar_sprite:setRect(0, -25, 768, 50)

  gamebar_prop = MOAIProp2D.new()
  gamebar_prop:setDeck(gamebar_sprite)
  gamebar_prop:setBlendMode(MOAIProp.GL_SRC_ALPHA, MOAIProp.GL_ONE_MINUS_SRC_ALPHA)
  gamebar_prop:setLoc(0, -40)
  MainLayer:insertProp(gamebar_prop)
  
  doneButton = MOAIGfxQuad2D.new()
  doneButton:setTexture("resources/graphics/ingame_menu/done.png")
  doneButton:setRect(-205, -80, 0, 0)

  doneButtonProp = MOAIProp2D.new()
  doneButtonProp:setDeck(doneButton)
  doneButtonProp:setLoc((baseScreenWidth / 2) + 102, -(baseScreenHeight - 90))
  doneButtonProp:setPriority(25000000)
  doneButtonProp:moveLoc(0, -90)
  MainLayer:insertProp(doneButtonProp)
  
  playerTextbox = MOAITextBox.new()
  playerTextbox:setStyle(style)
  playerTextbox:setRect(20, 45, -170, 0)
  playerTextbox:setAlignment(MOAITextBox.LEFT_JUSTIFY, MOAITextBox.CENTER_JUSTIFY)
  playerTextbox:setYFlip(true)
  
  goldTextBox = MOAITextBox.new()
  goldTextBox:setStyle(style)
  goldTextBox:setRect(375, 45, -450, 0)
  goldTextBox:setAlignment(MOAITextBox.LEFT_JUSTIFY, MOAITextBox.CENTER_JUSTIFY)
  goldTextBox:setYFlip(true)
  
  turnTextbox = MOAITextBox.new()
  turnTextbox:setStyle(style)
  turnTextbox:setRect(470, 45, -650, 0)
  turnTextbox:setAlignment(MOAITextBox.LEFT_JUSTIFY, MOAITextBox.CENTER_JUSTIFY)
  turnTextbox:setYFlip(true)
  
  endTurnTextbox = MOAITextBox.new()
  endTurnTextbox:setStyle(style)
  endTurnTextbox:setRect(275, -250, -100, -100)
  endTurnTextbox:setYFlip(true)
  endTurnTextbox:setString("Player 1 it's your turn!")
  endTurnTextbox:setVisible(false)
  
  local speakerOn = MOAIGfxQuad2D.new()
  speakerOn:setTexture("resources/graphics/ingame_menu/speaker_on.png")
  speakerOn:setRect(-35, -35, 0, 0)
  
  speakerOnProp = MOAIProp2D.new()
  speakerOnProp:setDeck(speakerOn)
  speakerOnProp:setLoc(750, -8)
  MainLayer:insertProp(speakerOnProp)
  
  local speakerOff = MOAIGfxQuad2D.new()
  speakerOff:setTexture("resources/graphics/ingame_menu/speaker_off.png")
  speakerOff:setRect(-35, -35, 0, 0)

  speakerOffProp = MOAIProp2D.new()
  speakerOffProp:setDeck(speakerOff)
  speakerOffProp:setLoc(750, -8)
  speakerOffProp:setVisible(false)
  MainLayer:insertProp(speakerOffProp)
  
  local settings = MOAIGfxQuad2D.new()
  settings:setTexture("resources/graphics/ingame_menu/settings.png")
  settings:setRect(-35, -35, 0, 0)
  
  settingsProp = MOAIProp2D.new()
  settingsProp:setDeck(settings)
  settingsProp:setLoc(690, -8)
  MainLayer:insertProp(settingsProp)
  
  local goldcoins = MOAIGfxQuad2D.new()
  goldcoins:setTexture("resources/graphics/ingame_menu/goldcoin.png")
  goldcoins:setRect(-35, -35, 0, 0)
  
  goldcoinsProp = MOAIProp2D.new()
  goldcoinsProp:setDeck(goldcoins)
  goldcoinsProp:setLoc(350, -8)
  MainLayer:insertProp(goldcoinsProp)
  
  if audioTurnedOn == false then
    speakerOnProp:setVisible(false)
    speakerOffProp:setVisible(true)
  end

  local renderTableTextboxen = {playerTextbox, goldTextBox, turnTextbox, endTurnTextbox}
  renderLayerTable[2] = renderTableTextboxen  
  --renderLayerTable[3] = unitHealthBoxRenderTable
  --renderLayerTable[4] = captureRenderTable
  startGameLoop()
  
  local gold = 0
    for k, v in pairs(pl1:getOwnedBuildingsList()) do
      local temp = v:getIncomePerTurn()
      gold = gold + temp
    end
  pl1:increaseGold(gold)
  
  if currentPlayerTurn == pl1 and tutRecruit1 == false and chosenLevel == 1 then
    tutRecruit1 = true
    local image = MOAIGfxQuad2D.new()
    image:setTexture("resources/graphics/ingame_menu/tutorialred1.png")
    image:setRect(200, -200, 500, -75)

    tutRed1 = MOAIProp2D.new()
    tutRed1:setDeck(image)
    tutRed1:setPriority(1000)
    MainLayer:insertProp(tutRed1)
  end
end

function StartGame:initProps(levelId)
  MainLayer:setPartition(gamePartition)
  x1 = 0
  y1 = -sizeMenuBalk
  x2 = 64
  y2 = -64 - sizeMenuBalk
  index = 1
  
  local map = Map.new()
  local level = map:getLevelById(levelId)
  
  local totalX = level[1]
  local totalY = level[2]
  local levelConfig = level[3]
  
  for v=1, totalY, 1 do
    self:setworld(levelConfig)
    index = index + 1
   
   for h=2, totalX, 1 do
     
    x1 = x1 + 64
    x2 = x2 +  64
    
    self:setworld(levelConfig)
        
    index = index + 1
   end
   
   x1 = 0
   y1 = (-64 * v)-sizeMenuBalk
   
   x2 = 64
   y2 = (-64 * (v + 1))-sizeMenuBalk
  end
end

function StartGame:setworld(__level)
    local tile = __level[index]
    local world = tile:getWorldObject()
    local building = tile:getBuilding()
    local unit = tile:getUnit()

    local soortWorldObject = world:getType()
    local soortBuilding = nil
    local soortUnit = nil
    
    local spriteWorld = MOAIGfxQuad2D.new()
    local spriteBuilding = nil
    local spriteUnit = nil
    
    if building ~= nil then
      spriteBuilding = MOAIGfxQuad2D.new()
      
      setTexturePlayer(spriteBuilding, building)
      spriteBuilding:setRect(x1, y2, x2, y1)
      
      if building:getType() == "Headquarters-player1" then
        pl1:setHQLocY(y1)
      end
      
      if building:getType() == "Headquarters-player2" then
        pl2:setHQLocY(y1)
      end
      
      propBuilding = MOAIProp2D.new()
      propBuilding:setPriority(20)
      propBuilding:setDeck(spriteBuilding)
      propBuilding:setBlendMode(MOAIProp.GL_SRC_ALPHA, MOAIProp.GL_ONE_MINUS_SRC_ALPHA)
      table.insert(allPropsTable, table.getn(rangeProps) + 1, propBuilding)
    end
    
    if unit ~= nil then
      spriteUnit = MOAIGfxQuad2D.new()
      
      setTexturePlayer(spriteUnit, unit)
      spriteUnit:setRect(x1, y2, x2, y1)
      
      propUnit = MOAIProp2D.new()
      propUnit:setPriority(30)
      propUnit:setDeck(spriteUnit)
      propUnit:setBlendMode(MOAIProp.GL_SRC_ALPHA, MOAIProp.GL_ONE_MINUS_SRC_ALPHA)
      table.insert(allPropsTable, table.getn(rangeProps) + 1, propUnit)
    end
    
    setTexturePlayer(spriteWorld, world)
    
    if soortWorldObject == "Road_Loop1" or soortWorldObject == "Road_Split1" then
      spriteWorld:setRect(x1, y2, x2, y1)
    elseif soortWorldObject == "Road_Loop2" then
      spriteWorld:setRect(x2, y2, x1, y1) 
    elseif soortWorldObject == "Road_Loop3" then
      spriteWorld:setRect(x1, y1, x2, y2)
    elseif soortWorldObject == "Road_Loop4"then
      spriteWorld:setRect(x2, y1, x1, y2)
    elseif soortWorldObject == "Road_Split2" then
      spriteWorld:setRect(x1, y1, x2, y2)
    elseif soortWorldObject == "Road_Split3" then
      spriteWorld:setTexture("resources/graphics/world/Road_Split3.png")
      spriteWorld:setRect(x1, y2, x2, y1)
    elseif soortWorldObject == "Road_Split4" then
      spriteWorld:setTexture("resources/graphics/world/Road_Split3.png")
      spriteWorld:setRect(x2, y2, x1, y1)
    else
      spriteWorld:setRect(x1, y2, x2, y1)
    end
  
    propWorld = MOAIProp2D.new()
    propWorld:setPriority(10)
    propWorld:setDeck(spriteWorld)
    propWorld:setBlendMode(MOAIProp.GL_SRC_ALPHA, MOAIProp.GL_ONE_MINUS_SRC_ALPHA)
    table.insert(allPropsTable, table.getn(rangeProps) + 1, propWorld)
    
    propToTileObject[propWorld] = tile
    
    MainLayer:insertProp(propWorld)
    if building ~= nil then
      propToTileObject[propBuilding] = building
      MainLayer:insertProp(propBuilding)
    end
    
    if unit ~= nil then
      propToTileObject[propUnit] = unit
      MainLayer:insertProp(propUnit)
    end    
    
    turnNightOn(tile)
end

function StartGame:startInstruction()
  local instruction = MOAITextBox.new()
  instruction:setStyle(style)
  instruction:setRect(250, 0, -380, -180)
  instruction:setAlignment(MOAITextBox.LEFT_JUSTIFY, MOAITextBox.CENTER_JUSTIFY)
  instruction:setYFlip(true)
  instruction:setPriority(1000)

  local timerBeforeBegin = MOAITimer.new()
  timerBeforeBegin:setMode(MOAITimer.NORMAL)
  timerBeforeBegin:setSpan(2)
  timerBeforeBegin:setListener(MOAITimer.EVENT_TIMER_END_SPAN, function()  
  
  if chosenLevel == 1 then
    if currentScreen == typesOfScreens["game"] then
      instruction:setString("Tap twice to end your turn")
      renderLayerTable[8] = instruction
      MainLayer:insertProp(instruction)
    end
  end

    local timer = MOAITimer.new()
    timer:setMode(MOAITimer.NORMAL)
    timer:setSpan(3)
    timer:setListener(MOAITimer.EVENT_TIMER_END_SPAN, function() 
          renderLayerTable[8] = nil
          MainLayer:removeProp(instruction)
    end)
    timer:start() 
  end)
  timerBeforeBegin:start()
  
  local timerSecondTip = MOAITimer.new()
  timerSecondTip:setMode(MOAITimer.NORMAL)
  timerSecondTip:setSpan(25)
  timerSecondTip:setListener(MOAITimer.EVENT_TIMER_END_SPAN, function()  
  
  if chosenLevel == 1 then
    if currentScreen == typesOfScreens["game"] then
      instruction:setRect(200, 0, -380, -180)
      instruction:setString("Attacking gives your units a bonus!")
      renderLayerTable[8] = instruction
      MainLayer:insertProp(instruction)
    end
  end
  
    local timer = MOAITimer.new()
    timer:setMode(MOAITimer.NORMAL)
    timer:setSpan(4)
    timer:setListener(MOAITimer.EVENT_TIMER_END_SPAN, function() 
          renderLayerTable[8] = nil
          MainLayer:removeProp(instruction)
    end)
    timer:start() 
  end)
  timerSecondTip:start()
  
  local timerThirdTip = MOAITimer.new()
  timerThirdTip:setMode(MOAITimer.NORMAL)
  timerThirdTip:setSpan(50)
  timerThirdTip:setListener(MOAITimer.EVENT_TIMER_END_SPAN, function()  
  
  
  if chosenLevel == 1 then
    if currentScreen == typesOfScreens["game"] then
      instruction:setRect(200, 0, -380, -180)
      instruction:setString("Place units on buildings for a defense bonus!")
      renderLayerTable[8] = instruction
      MainLayer:insertProp(instruction)
    end
  end
  
    local timer = MOAITimer.new()
    timer:setMode(MOAITimer.NORMAL)
    timer:setSpan(4)
    timer:setListener(MOAITimer.EVENT_TIMER_END_SPAN, function() 
          renderLayerTable[8] = nil
          MainLayer:removeProp(instruction)
    end)
    timer:start() 
  end)
  timerThirdTip:start()
end

 return StartGame